package com.zuimei.zuimei.fragments;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zuimei.zuimei.R;
import com.zuimei.zuimei.activities.Item3DescActivity;
import com.zuimei.zuimei.adapters.Item3RecycleViewItemAdapter;
import com.zuimei.zuimei.base.BaseFragment;
import com.zuimei.zuimei.beans.FindAppItem1Bean;
import com.zuimei.zuimei.http.HttpHelper;
import com.zuimei.zuimei.jsonutil.ParseFindAppJson;
import com.zuimei.zuimei.utils.ConstantUtil;

import java.util.List;

import butterknife.Bind;

/**
 * Created by Jogger on 2017/1/16.
 * 发现应用viewpager内fragment
 */

public class FindAppItem3Fragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener{
    @Bind(R.id.rv_item)
    RecyclerView rv_item;
    @Bind(R.id.srl_refresh)
    SwipeRefreshLayout srl_refresh;
    private List<FindAppItem1Bean.AppsBean> mAppsBeanList;
    private Item3RecycleViewItemAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.rv_find_app_item;
    }

    @Override
    public void loadData() {
        srl_refresh.setOnRefreshListener(this);
        new HttpHelper(mActivity).getData(ConstantUtil.FIND_APP_ITEM3_URL, new HttpHelper
                .OnLoadCompletedListener() {


            @Override
            public void onSuccess(String result) {
                mAppsBeanList = ParseFindAppJson.getItem3List(result);
                mAdapter = new Item3RecycleViewItemAdapter(mActivity);
                rv_item.setLayoutManager(new LinearLayoutManager(mActivity));
                rv_item.setAdapter(mAdapter);
                mAdapter.loadData(mAppsBeanList);
                mAdapter.setOnItemClickListener(new Item3RecycleViewItemAdapter.OnItemClickListener() {

                    @Override
                    public void onItemClick(View view, FindAppItem1Bean.AppsBean appBean) {
                        startNewActivity(Item3DescActivity.class,"app",appBean);
                    }
                });
            }

            @Override
            public void onFailure() {
                srl_refresh.setRefreshing(false);
            }
        });
    }

    @Override
    public void onRefresh() {
        new HttpHelper(mActivity).getRefreshData(ConstantUtil.FIND_APP_ITEM3_URL, new HttpHelper
                .OnLoadCompletedListener() {

            @Override
            public void onSuccess(String result) {
                mAppsBeanList = ParseFindAppJson.getItem3List(result);
                mAdapter.loadData(mAppsBeanList);
                srl_refresh.setRefreshing(false);
            }

            @Override
            public void onFailure() {

            }
        });
    }
}
